package com.recharge.web.controller;


import com.recharge.common.annotation.OperationLogDetail;
import com.recharge.common.domain.PageDO;
import com.recharge.common.enums.OperationLogType;
import com.recharge.common.result.ResultDO;
import com.recharge.common.result.web.ObjectResponse;
import com.recharge.domain.query.GoodsMerchantQueryDO;
import com.recharge.domain.vo.GoodsMerchantDO;
import com.recharge.service.GoodsMerchantService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Reagan
 * @since 2020-09-28
 */
@RestController
@RequestMapping("/goods-merchant")
public class GoodsMerchantController extends BaseController{

    @Autowired
    private GoodsMerchantService goodsMerchantService;

    @RequiresPermissions("goods-merchant:page")
    @RequestMapping(value="/page.html", method = RequestMethod.POST)
    @ApiOperation(value="分页查询供货商品关联", notes="")
    public @ResponseBody
    ObjectResponse<PageDO> page(@RequestBody GoodsMerchantQueryDO query) {

        ObjectResponse<PageDO> response = new ObjectResponse<>();

        ResultDO result = goodsMerchantService.find(query);

        if(result.isSuccess()) {
            response.setData((PageDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
        } else {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }

        return response;
    }


    @RequiresPermissions("goods-merchant:add")
    @OperationLogDetail(detail = "添加代理商品关联", operationType = OperationLogType.ADD)
    @RequestMapping(value="/add.html", method = RequestMethod.POST)
    @ApiOperation(value="添加代理商品关联", notes="")
    public @ResponseBody  ObjectResponse<Object> add(@RequestBody GoodsMerchantDO param) {
        ObjectResponse<Object> response = new ObjectResponse<>();

        param.setModifier(getUserName());
        param.setCreator(getUserName());

        ResultDO result = goodsMerchantService.addBatch(param);
        if(result.isSuccess()) {
            response.setData(null);
        } else {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }


    @RequiresPermissions("goods-merchant:delete")
    @OperationLogDetail(detail = "删除代理商品组关联", operationType = OperationLogType.DELETE)
    @RequestMapping(value="/delete.html", method = RequestMethod.POST)
    @ApiOperation(value="删除代理商品组关联", notes="")
    public @ResponseBody  ObjectResponse<Object> delete(@RequestBody GoodsMerchantDO param) {
        ObjectResponse<Object> response = new ObjectResponse<>();

        ResultDO result = goodsMerchantService.remove(param.getMerchantId(), param.getGroupId(), getUserName());
        if(result.isSuccess()) {
            response.setData(null);
        } else {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("goods-merchant:update")
    @OperationLogDetail(detail = "修改供货商品关联", operationType = OperationLogType.UPDATE)
    @RequestMapping(value="/update.html", method = RequestMethod.POST)
    @ApiOperation(value="修改供货商品关联", notes="")
    public @ResponseBody  ObjectResponse<Object> update(@RequestBody GoodsMerchantDO param) {
        ObjectResponse<Object> response = new ObjectResponse<>();
        param.setModifier(getUserName());
        ResultDO result = goodsMerchantService.edit(param);
        if(result.isSuccess()) {
            response.setData(null);
        } else {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("goods-merchant:updateStatus")
    @RequestMapping(value="/updateStatus.html", method = RequestMethod.POST)
    @ApiOperation(value="代理商品组维护", notes="")
    public @ResponseBody
    ObjectResponse updateStatus(@RequestBody GoodsMerchantDO domain) {

        ObjectResponse response = new ObjectResponse();


        ResultDO result = goodsMerchantService.updateStatus(domain.getMerchantId(), domain.getGoodsIds(), domain.getStatus(), getUserName());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("goods-merchant:updateDiscountBatch")
    @RequestMapping(value="/updateDiscountBatch.html", method = RequestMethod.POST)
    @ApiOperation(value="批量修改代理折扣", notes="")
    public @ResponseBody  ObjectResponse<Object> updateDiscountBatch(@RequestBody GoodsMerchantDO param) {
        ObjectResponse<Object> response = new ObjectResponse<>();
        ResultDO result = goodsMerchantService.updateDiscountBatch(param.getGroupId(), param.getMerchantId(), param.getDiscount(), getUserName());
        if(result.isSuccess()) {
            response.setData(result.getModel(ResultDO.FIRST_MODEL_KEY));
        } else {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("goods-merchant:getGoodsGroup")
    @RequestMapping(value="/getGoodsGroup.html", method = RequestMethod.POST)
    @ApiOperation(value="查询指定代理商下面的商品组", notes="")
    public @ResponseBody  ObjectResponse<Object> getGoodsGroup(@RequestBody GoodsMerchantDO param) {
        ObjectResponse<Object> response = new ObjectResponse<>();
        ResultDO result = goodsMerchantService.getGoodsGroup(param.getMerchantId());
        if(result.isSuccess()) {
            response.setData(result.getModel(ResultDO.FIRST_MODEL_KEY));
        } else {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }
}

